<template>
    <el-aside width="230px" :style="{ height: menuHeight + 'px', overflowY: 'auto' }">
      <el-menu
          :default-active="activeMenu"
          @select="handleSelect"
      >
        <!-- 回放录像 -->
        <el-sub-menu v-for="(months, year) in timeList" :key="year" :index="year">
          <template #title>
            <el-icon><Document /></el-icon>
            {{ year }} 年
          </template>
          <el-sub-menu v-for="(days, month) in months" :key="`${year}-${month}`" :index="`${year}-${month}`">
            <template #title>
              <el-icon><Document /></el-icon>
              {{ month }} 月
            </template>
            <el-sub-menu v-for="(times, day) in days" :key="`${year}-${month}-${day}`" :index="`${year}-${month}-${day}`">
              <template #title>
                <el-icon><Document /></el-icon>
                {{ day }} 日
              </template>
              <el-menu-item v-for="time in times" :key="time" :index="`/monitor/history/${time}`">
                <el-icon><VideoCameraFilled /></el-icon>
                {{ time }}
              </el-menu-item>
            </el-sub-menu>
          </el-sub-menu>
        </el-sub-menu>
      </el-menu>
    </el-aside>
</template>

<script lang="ts" setup>
// Element Plus 图标
import { ElMenu, ElMenuItem, ElIcon } from 'element-plus';
import { Document, VideoCameraFilled } from "@element-plus/icons-vue";
import { useRouter, useRoute } from "vue-router";
import { computed, onMounted, onBeforeUnmount, ref } from "vue";
import axios from 'axios';

const router = useRouter();
const route = useRoute();

// 当前激活菜单
const activeMenu = computed(() => route.path);

// 菜单点击跳转
const handleSelect = (index: string) => {
  router.push(index);
};

console.log(route.params.time);

const timeList = ref([]);
//const timeList = ref(['2025-04-04-16', '2025-04-04-17', '2025-04-04-18']);
const fetchTimeList = async () => {
  try {
    const response = await axios.get('/timevideolist');
    const jsonData = response.data;
    timeList.value = response.data;
  } catch (error) {
    console.error('获取时间列表失败:', error);
  }
};

const menuHeight = ref(window.innerHeight -60); // 初始高度
const handleResize = () => {
  const windowHeight = window.innerHeight -60;
  // 这里可以根据需要设置菜单高度占窗口高度的比例或其他逻辑
  menuHeight.value = windowHeight; // 示例：占窗口高度的40%
};

onMounted(() => {
  fetchTimeList();
  window.addEventListener('resize', handleResize);
});

onBeforeUnmount(() => {
  window.removeEventListener('resize', handleResize);
});

</script>
